import 'dart:io';

import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/utils/common_util/toast/custom_toast.dart';
import 'package:star_author/widgets/dialog/dialog_type.dart';
import 'package:star_author/widgets/dialog/sure_dialog.dart';
import 'package:permission_handler/permission_handler.dart';

class LivePermissionUtil {
  //检测是否有弹框权限 没有弹框权限在退出页面的时候弹出来
  static checkSystemAlertWindowPermission(
      Function(bool isGranted) callBack) async {
    if (Platform.isAndroid && StreamConf.uid != -1) {
      await dialogPermission.then(
        (value) async {
          if (value.isGranted) {
            //同意
            callBack(true);
          } else {
            DialogUtil.showMyDialog(
              dialogType: DialogType.system_alert_window_tips,
              leftButtonOnTap: () {
                callBack(false);
                CustomToast.show("您取消了操作!");
              },
              rightButtonOnTap: () async {
                if (await initPermission().isGranted) {
                  callBack(true);
                } else {
                  callBack(false);
                  CustomToast.show("您取消了操作!");
                }
              },
            );
          }
        },
      );
    }
  }

  static Future<PermissionStatus> initPermission() async {
    return Permission.systemAlertWindow.request();
  }

  //是否有悬浮窗权限
  static Future<PermissionStatus> get dialogPermission {
    return Permission.systemAlertWindow.status;
  }
}
